﻿package com._public._display 
{
	import flash.display.Sprite;
	import flash.events.Event;
	
	/**
	 * 带自我毁灭的基类,删除对象同时删除子对象/属性/侦听
	 * @author WZH(shch8.com)
	 */
	public class CSprite extends Sprite
	{
		
		    private var lis:Array;
			public function CSprite(){
			  lis = new Array()
			  addEventListener(Event.REMOVED,remove)
			}
			override public function addEventListener(type:String, listener:Function, useCapture:Boolean = false, priority:int = 0, useWeakReference:Boolean = false):void{
			  lis.push([type,listener,useCapture])
			  super.addEventListener(type,listener,useCapture,priority,useWeakReference)
			}
			private function remove(e:Event):void{
			  if(e.currentTarget != e.target)return
			  //删除子对象
			  while(numChildren > 0){
				removeChildAt(0)
			  }
			  //删除动态属性
			  for(var k:String in this){
				delete this[k]
			  }
			  //删除侦听
			  for(var i:uint=0;i<lis.length;i++){
				removeEventListener(lis[i][0],lis[i][1],lis[i][2])
			  }
			  lis = null
			}
		}
	}
	



